package com.sayice.gomoku.dao.impl;

import com.sayice.gomoku.dao.UserDao;
import com.sayice.gomoku.pojo.User;

/**
 * 用户数据库操作
 */
public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public int add(User user) {
        String sql = "insert into user(name, password) values(?, ?)";
        return update(null, sql, user.getName(), user.getPassword());
    }
    
    @Override
    public int updateBoardId(int userId, int boardId) {
        String sql = "update user set boardId = ? where id = ?";
        return update(null, sql, boardId, userId);
    }
    
    @Override
    public int addPlayCount(int id) {
        String sql = "update user set playCount = playCount + 1 where id = ?";
        return update(null, sql, id);
    }
    
    @Override
    public int addWinCount(int id) {
        String sql = "update user set winCount = winCount + 1 where id = ?";
        return update(null, sql, id);
    }
    
    @Override
    public User queryById(int id) {
        String sql = "select id, name, password, boardId, playCount, winCount from user where id = ?";
        return queryForOne(User.class, sql, id);
    }
    
    @Override
    public User queryByName(String name) {
        String sql = "select id, name, password, boardId, playCount, winCount from user where name = ?";
        return queryForOne(User.class, sql, name);
    }
    
    @Override
    public User queryByNameAndPassword(String name, String password) {
        String sql = "select id, name, password, boardId, playCount, winCount from user where name = ? and password = ?";
        return queryForOne(User.class, sql, name, password);
    }
}
